package org.andes.kownledge;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import lisp.SExp;
import lisp.Tool;

import org.andes.help.struct.IContent;

/**
 * Created by IntelliJ IDEA.
 * User: wenfeix
 * Date: 2011-2-1
 * Time: 9:42:42
 * To change this template use File | Settings | File Templates.
 */
public class BubbleGraph implements IContent {
    private List<Qnode> qnodes=new ArrayList<Qnode>();
    private List<Enode> enodes=new ArrayList<Enode>();
    
    public BubbleGraph(){
    	System.out.println("init BubbleGraph");
    }
    
    //-------------------------------------------------------------------
    // Bubblegraph Input functions
    // The functions in this section are devoted to reading in a
    // bubblegraph from an mreadable stream form and re-creating it.
    private void readMeadableBubbleGraph(String s){}


   /* ;;---------------------------------------------------------------
;; Qnode reading code.
(defun read-mreadable-qnode-lst (S)
  "Read in an mreadable list of Qnodes."
  (mg-srt S '<Qnodes>)
  (do (N R)
      ((null (setq N (read-mreadable-qnode S))) R)
    (setq R (append R (list N))))) */

    private void readMreadableQnodeLst(String s){

    }


    /*
    (defun read-mreadable-qnode (S)
  "Read an Mreadable qnode from the stream."
  (when (not (equal (mg-srt2 S '<Qnode> '</Qnodes>) '</Qnodes>))
    (let ((N (make-qnode :Symbol (mg-sr S)
			 :gindex (mg-sr S)
			 :vindex (mg-sr S)
			 :Exp (mg-sr S)
			 :var (mg-sr S)
			 :Eqns (mg-sr S)
			 :marks (mg-sr S)
			 :path (psmg->help-psmg (mg-sr S))
			 :assumptions (mg-sr S)
			 :Entries (mg-sr S))))

      (mg-srt S '</Qnode>)
      N)))
     */

    private void readMreadableQnode(String s){

    }

    public void read(String s) {
       
    }
    /*
    read-mreadable-bubblegraph
     */
    public void readContent(InputStream S) throws Exception {
       mg_srt(S,"<BubbleGraph>");
           read_mreadable_qnode_lst(S);
           read_mreadable_enode_lst(S);
        mg_srt(S,"</BubbleGraph>");
       ;
    }


    /*
    ;;---------------------------------------------------------------
;; Qnode reading code.
(defun read-mreadable-qnode-lst (S)
  "Read in an mreadable list of Qnodes."
  (mg-srt S '<Qnodes>)
  (do (N R)
      ((null (setq N (read-mreadable-qnode S))) R)
    (setq R (append R (list N)))))
     */
     private void read_mreadable_qnode_lst(InputStream S) throws Exception {
          mg_srt(S,"<Qnodes>");         
        Qnode qnode;
        String name;
        do{

        	name=Tool.mg_sr(S);
        	if(name.equals("</Qnodes>"))
        		break;
        	else if(!name.equals("<Qnode>"))
        		throw new Exception("mms");
        	qnode=read_mreadable_qnode(S);
        	qnodes.add(qnode);
        	mg_srt(S,"</Qnode>");
        }
        while (true);     
    }
    
	private Qnode read_mreadable_qnode(InputStream S) throws Exception {
		Qnode qnode;
		qnode = new Qnode();
		qnode.readContent(S);
		return qnode;

	}

	private Enode read_mreadable_enode(InputStream S) throws Exception {

		Enode enode = new Enode();
		enode.readContent(S);
		return enode;


	}
    
	
	 private void read_mreadable_enode_lst(InputStream S) throws Exception {
         mg_srt(S,"<Enodes>");         
         Enode enode;
       String name;
       do{

       	name=Tool.mg_sr(S);
       	if(name.equals("</Enodes>"))
       		break;
       	else if(!name.equals("<Enode>"))
       		throw new Exception("mms");
       	enode=read_mreadable_enode(S);
       	enodes.add(enode);
       	mg_srt(S,"</Enode>");
       }
       while (true);     
   }

    private void  mg_srt(InputStream S,String C) throws Exception{
        SExp sexp=SExp.INPUT(S);
         if(!C.equals(sexp.GetValue()))
             throw new Exception();
   }

    public List<Qnode> getQnodes() {
        return qnodes;
    }

   

    public List<Enode> getEnodes() {
        return enodes;
    }

    public Enode match_algebra_enode(String algebra){
         for(Enode enode:enodes){
            if(algebra.equals(enode.getAlgebra())) {
                return enode;
            }
         }
        return null;
    }

   
    public BubbleNode get_nth_bgnode(int index){
    	int lenq=qnodes.size();
    	int lene=enodes.size();
    	if(index<lenq)
    		return qnodes.get(index);
    	else
    		return qnodes.get(index-lenq);
        
    }
}
